<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\TaskTest_statu;
use App\Facades\AdminC;
use App\Http\Controllers\Controller;
use App\Models\TaskTest;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Illuminate\Support\Facades\Input;

class TaskTestController extends Controller
{
    use ModelForm;

    public function index()
    {
        return AdminC::content(function(Content $content){
            $content->header('试玩任务列表');
            $content->description('列表');
            $content->body($this->grid());
        });
    }

    protected function grid()
    {
        return AdminC::grid(TaskTest::class, function(Grid $grid){
            $grid->id('Id');
            $grid->name('小程序');
            $grid->app_id('小程序Id');
            $grid->intro('描述/规则');
            $grid->status('状态')->display(function($status){
                return $this->status_arr[$status];
            });
            $grid->bi('奖励')->display(function($bi){
                return $bi.'金币';
            });
            $grid->play_nums('试玩次数');

            $grid->actions(function($actions){
                $info =  $actions->row;
                if($info->status){
                    $actions->prepend(new TaskTest_statu($actions->getKey(), 1));
                }else{
                    $actions->prepend(new TaskTest_statu($actions->getKey(), 0));
                }
            });
            $grid->disableExport();
        });
    }

    public function opreat()
    {
        $input = Input::all();
        $model = TaskTest::find($input['id']);
        $model->status = $input['type']==1 ? 0 : 1;
        $res = $model->save();
        if($res) {
            return response()->json([
                'code' => 1,
                'msg' => '成功'
            ]);
        }else{
            return response()->json([
                'code' => 0,
                'msg' => '失败'
            ]);
        }
    }

    public function edit($id)
    {
        return AdminC::content(function(Content $content) use($id) {
            $content->header('试玩任务');
            $content->description('编辑');
            $content->body($this->form()->edit($id));
        });
    }

    public function create()
    {
        return AdminC::content(function(Content $content){
            $content->header('试玩任务');
            $content->description('新增');
            $content->body($this->form());
        });
    }

    protected function form()
    {
        return AdminC::form(TaskTest::class, function(Form $form){
            $form->display('id');
            $form->text('name','小程序');
            $form->text('app_id','小程序Id');
            $form->textarea('intro', '描述/规则');
            $form->text('bi','奖励金币');
            $form->select('status', '状态')->options($form->model()->status_arr);
            $form->display('created_at' , '创建时间');
            $form->display('updated_at' , '更新时间');
        });
    }
}
